<?php
/**
 * Classe contains Frequently used methods
 * @author Edio Hanoway Kepke
 * @version 1.0
 * @access public 
 */
class Fum
{                                                             
    
    /**
    * Return the key used in the hash
    * @access public
    * @return String
    */
    public static function getKey()
    {
        return '@qu1r1s_t0ddynhO';
    }
    
    /**
    * Redirect to url passed
    * @access public
    * @param $url String Url to redirect
    * @return page
    */
    public static function redirectTo($url)
    {
        $CI = &get_instance();
        $CI->load->helper('url');        
        redirect($url);
    }
    
    /**
    * Return base url of the site
    * @access public    
    * @return String
    */
    static function getUrlBase()
    {
        $protocol = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === false) ? 'http' : 'https';
        $host = $_SERVER['HTTP_HOST'];
        $script = $_SERVER['SCRIPT_NAME'];
        return $protocol."://".$host.$script;
    }   
    
    
    /**
    * Print debug object
    * @access public 
    * @param $object Object Object that appears   
    * @return String
    */
    static function printToDebug($object)
    {
        return "<pre>".print_r($object, true)."</pre>";
    }  
    
    /**
    * Validates a Brazilian cpf
    * @access public 
    * @param $cpf The CFP for validation
    * @return Boolean
    */   
    public static function validCPF($cpf)
    {                   
        $cpf = str_replace(array("-", "."), "", $cpf);
                        
        if(!is_numeric($cpf)) 
        {
            return false;
        }
        
        if (strlen($cpf) != 11 || $cpf == '00000000000' || $cpf == '11111111111' || $cpf == '22222222222' || $cpf == '33333333333' || $cpf == '44444444444' || $cpf == '55555555555' || $cpf == '66666666666' || $cpf == '77777777777' || $cpf == '88888888888' || $cpf == '99999999999')
        {
            return false;
        }
        else
        {
            for ($t = 9; $t < 11; $t++) 
            {
                for ($d = 0, $c = 0; $c < $t; $c++) 
                {
                    $d += $cpf{$c} * (($t + 1) - $c);
                }

                $d = ((10 * $d) % 11) % 10;

                if ($cpf{$c} != $d) 
                {
                    return false;
                }
            }
                        
            return true;
        }                                                           
    } 
    
    
    /**
    * Encrypt one string using base64_encode, md5 and secret key
    * @access public 
    * @param $str String to encrypt
    * @return String
    */ 
    public static function encrypt($str)
    {                        
        return base64_encode(base64_encode($str).md5(Fum::getKey()));        
    }
    
    
    /**
    * Decrypt a string that was encrypted using encrypt method 
    * @access public 
    * @param $str String to dencrypt
    * @return String
    */ 
    public static function decrypt($str)
    {                       
        $arrayExplode = explode(md5(self::getKey()), base64_decode($str));        
        return base64_decode($arrayExplode[0]);                                
    }
            
    /**
    * Convert word
    * @access public 
    * @param $opt Map contend the options of conversion: [removeAccentuation = (true|false), uppercase = (true|false), lowercase = (true|false), removeSpace = character]
    * @return String
    */
    public static function cleanWord($word,$opt,$enc = 'UTF-8')
    {    
        if(isset($opt['uppercase']))
        {
            if($opt['uppercase'])
            {
                $word = strtr(strtoupper($word),"àáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ","ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß");                 
            }                        
        }
        
        if(isset($opt['lowercase']))
        {
            if($opt['lowercase'])
            {
                $word = strtr(strtolower($word),"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞß","àáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ");;
            }                        
        }
        
        if(isset($opt['removeSpace']))
        {
            if($opt['removeSpace'])
            {
                $word = str_replace(" ", $opt['removeSpace'], $word);
            }                        
        }
                        
	if(isset($opt['removeAccentuation']))
        {
            if($opt['removeAccentuation'])
            {
                $acentos = array
                (
                    'A' => '/&Agrave;|&Aacute;|&Acirc;|&Atilde;|&Auml;|&Aring;/',
                    'a' => '/&agrave;|&aacute;|&acirc;|&atilde;|&auml;|&aring;/',
                    'C' => '/&Ccedil;/',
                    'c' => '/&ccedil;/',
                    'E' => '/&Egrave;|&Eacute;|&Ecirc;|&Euml;/',
                    'e' => '/&egrave;|&eacute;|&ecirc;|&euml;/',
                    'I' => '/&Igrave;|&Iacute;|&Icirc;|&Iuml;/',
                    'i' => '/&igrave;|&iacute;|&icirc;|&iuml;/',
                    'N' => '/&Ntilde;/',
                    'n' => '/&ntilde;/',
                    'O' => '/&Ograve;|&Oacute;|&Ocirc;|&Otilde;|&Ouml;/',
                    'o' => '/&ograve;|&oacute;|&ocirc;|&otilde;|&ouml;/',
                    'U' => '/&Ugrave;|&Uacute;|&Ucirc;|&Uuml;/',
                    'u' => '/&ugrave;|&uacute;|&ucirc;|&uuml;/',
                    'Y' => '/&Yacute;/',
                    'y' => '/&yacute;|&yuml;/',
                    'a.' => '/&ordf;/',
                    'o.' => '/&ordm;/'
                );

                $word = preg_replace($acentos, array_keys($acentos), htmlentities($word,ENT_NOQUOTES, $enc));                             
            }            
        }
        
	return $word;      
    }
    
    
     /**
    * Return content array containing error object and data object
    * @access public
    * @param $error Error number
    * @param $data Data
    * @return Array
    */
    public static function getArrayReturn($error, $data = null)
    {        
        return array('content' => array
                                        (
                                            'error' => Error::getError($error),
                                            'data' => $data
                                        )            
                    );    
    }
               
    /**
    * Return data of other Web Service using curl
    * @access public
    * @param $url String Url of the service    
    * @param $usePost boolean method post is used
    * @param $data Dictionary Variables to be send
    * @return Object
    */
}


